import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { addOutBoundAgentApi, modifyOutBoundAgentApi } from "@/api/configCenter/outBoundAgent";
import { Ref } from "vue";
export default function OperationHook(state: {
  formDataRef: Ref<any>;
  formState: OutBoundAgent.AddForm;
  emit: any;
  passData: Ref<OutBoundAgent.ListRows>;
  type: Ref<number>;
  loading: Ref<boolean>;
  accountOpts: Ref<Base.GeneraDataType[]>;
}) {
  const { formDataRef, formState, emit, passData, type, loading, accountOpts } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      const { accountName } = accountOpts.value.find((t: any) => t.id === formState.accountId) || {};
      let requestFn;
      const params = {
        ...formState,
        accountName
      };
      if (type.value === 1) {
        requestFn = addOutBoundAgentApi;
        Reflect.deleteProperty(params, "id");
      } else {
        requestFn = modifyOutBoundAgentApi;
        params.id = passData.value.id;
      }
      loading.value = true;
      requestFn(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
            resetFields();
            formDataRef.value.clearValidate();
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const addCompanyKeyListHandle = () => {
    formState.companyKeyList.push({
      companyName: "",
      companyId: "",
      companyKey: ""
    });
  };
  const deleteCompanyKeyListHandle = (index: number) => {
    formState.companyKeyList.splice(index, 1);
  };

  const addHandle = () => {
    formState.bizOutWarehouseBrokerContactList.push({
      contact: "",
      phone: "",
      address: "",
      provinceCode: "",
      provinceName: "",
      cityCode: "",
      cityName: "",
      areaCode: "",
      areaName: "",
      addressModel: []
    });
  };
  const deleteHandle = (index: number) => {
    formState.bizOutWarehouseBrokerContactList.splice(index, 1);
  };

  const changeWarehouseAddressHandle = (value: Array<string>, selectedOptions: Array<Base.GeneraDataType>, row: OutBoundAgent.ListRows) => {
    if (!value || !value.length) {
      row.provinceCode = "";
      row.provinceName = "";
      row.cityCode = "";
      row.cityName = "";
      row.areaCode = "";
      row.areaName = "";
    } else {
      const selectedProvince = selectedOptions[0];
      const selectedCity = selectedOptions[1];
      const selectedArea = selectedOptions[2];
      row.provinceCode = selectedProvince.code;
      row.provinceName = selectedProvince.name;
      row.cityCode = selectedCity.code;
      row.cityName = selectedCity.name;
      row.areaCode = selectedArea.code;
      row.areaName = selectedArea.name;
    }
  };
  const changeAddressHandle = (value: Array<string>, selectedOptions: Array<Base.GeneraDataType>) => {
    if (!value || !value.length) {
      formState.provinceCode = "";
      formState.provinceName = "";
      formState.cityCode = "";
      formState.cityName = "";
      formState.areaCode = "";
      formState.areaName = "";
    } else {
      const selectedProvince = selectedOptions[0];
      const selectedCity = selectedOptions[1];
      const selectedArea = selectedOptions[2];
      formState.provinceCode = selectedProvince.code;
      formState.provinceName = selectedProvince.name;
      formState.cityCode = selectedCity.code;
      formState.cityName = selectedCity.name;
      formState.areaCode = selectedArea.code;
    }
  };
  const changeCompany = (e: string, options: { label: string; value: string }, index: number) => {
    formState.companyKeyList[index].companyName = options.label;
  };
  return {
    resetHandle,
    submitHandle,
    resetFields,
    validateZhInputHandle,
    addHandle,
    deleteHandle,
    changeWarehouseAddressHandle,
    changeAddressHandle,
    addCompanyKeyListHandle,
    deleteCompanyKeyListHandle,
    changeCompany
  };
}
